﻿using UnityEngine;
using System.Collections;

public class AstonScript : StateMachineBehaviourEx {
	public float speedX = 10f;
	public int HP = 100;
	public float leftWallCoordX = 6.75f;
	public float rightWallCoordX = 29.25f;

	public GameObject attackObj;

	//variables for Referencing
	Animator _animator;
	BoxCollider2D _astonCollider;
	BoxCollider2D _attackObjCollider;
	CameraController _cameraController;
	CharacterSkill _characterSkill;

	//other variables
	int curAttackCount=0;
	int newAttackCount=0;
	bool isFacingRight=true;

	const float keyCombination_skill1 = 0.314f;	//left, up, right

	bool testing = true;


	// Use this for initialization
	void Start () {
		_cameraController = GetComponent<CameraController>();

		_astonCollider = GetComponent<BoxCollider2D>();

		_animator = GetComponent<Animator>();
		_attackObjCollider = attackObj.GetComponent<BoxCollider2D>();

		_characterSkill = GetComponent<CharacterSkill>();

	}

	void FixedUpdate(){
		stopCameraMovementIfNecessary();

		if( curAttackCount != newAttackCount ){
			curAttackCount = newAttackCount;
			_animator.SetInteger("attack", curAttackCount);

			if( curAttackCount > 0 ){
				_attackObjCollider.enabled = true;
			}
		}
		


		if(_characterSkill.ifKeyCombinationIsActivated()){

			if(_characterSkill.getKeyCombination() == keyCombination_skill1){
				_animator.SetInteger("skillId", 1);
			}else{
				_animator.SetInteger("skillId", 0);
			}

		}else{
			#region "Movement"
			//1. get movement value
			float movement = Input.GetAxis("Horizontal");
			float absoluteVal = Mathf.Abs(movement);

			//2. set animation
			_animator.SetFloat("moveSpeed", absoluteVal);
			
			//3. set direction
			if(movement<0 && isFacingRight){
				flip ();
			}else if(movement>0 && !isFacingRight){
				flip();
			}
			
			//4. set physic/velocity movement
			Vector2 newVelocity = rigidbody2D.velocity;
			newVelocity.x = movement * speedX;
			rigidbody2D.velocity = newVelocity;
			
			//5. set collider size change
			if(absoluteVal > 0.9){
				_astonCollider.size = new Vector2(1.45f, 1.78f);
			}else{
				_astonCollider.size = new Vector2(1.27f, 1.78f);
			}
			#endregion
		}

	}//FixedUpdate()

	// Update is called once per frame
	void Update () {

		#region "Attack"
		if(Input.GetButton("Fire1")){
			newAttackCount = 1;

		}else if(Input.GetButtonUp("Fire1")){
			Invoke("stopAttack", 0.4f);	//run stopAttack() after 0.4 second
		}
		#endregion

	}//Update()

	void stopCameraMovementIfNecessary(){
		//if camera hit the "wall"
		if( transform.position.x <= leftWallCoordX ||
		    transform.position.x >= rightWallCoordX ){
			_cameraController.isFollowingCharacter = false;
		}else{
			_cameraController.isFollowingCharacter = true;
		}
	}

	void flip(){
		isFacingRight = !isFacingRight;
		transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
	}

	void stopAttack(){
		newAttackCount = 0;
	}

	void stopSkill(){
		_characterSkill.resetKeyCombinationDetails();
		_animator.SetInteger("skillId", 0);
		Debug.Log("in stopSkill()");
	}

	//use onTriggerEnter instead of onCollision because AttackObj has IsTrigger enabled.
	void OnTriggerEnter2D(Collider2D collidedObj){
		if( "MonsterAttack".Equals(collidedObj.gameObject.tag) ){
			int damageReceived = collidedObj.gameObject.GetComponent<AttackObjScript>().damageDeal;
			HP -= damageReceived;
		}
	}
}//class
